import Vue from 'vue'
import VueRouter from 'vue-router'
import Manage from '../views/Manage.vue'
import Regist from "@/views/Regist";

Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   redirect: '/login',
  //   name: 'Manage',
  //   component: Manage,
  //   children: [
  //     { path: 'user', name: '用户管理', component: () => import('../views/User') },
  //     { path: 'file', name: '文件阅览', component: () => import('../views/File') },
  //     { path: 'filedetails', name: '文件详情', component: () => import('../views/FileDetails') },
  //     { path: 'role', name: '角色管理', component: () => import('../views/Role') },
  //     { path: 'menu', name: '菜单管理', component: () => import('../views/Menu') },
  //     { path: 'home', name: '欢迎', component: () => import('../views/Home') },
  //     { path: 'article', name: '博文阅览', component: () => import('../views/Article') },
  //     { path: 'person', name: '个人信息', component: () => import('../views/Person') },
  //     { path: 'my-articles', name: '我的博文', component: () => import('../views/MyArticles') },
  //     { path: 'my-files', name: '我的文件', component: () => import('../views/MyFiles') }
  //   ]
  // },
  { // 初始化路由
    path: '/',
    redirect: '/login',
    name: 'Init'
  },
  {
    path: '/login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/regist',
    name: 'Regist',
    component: () => import('../views/Regist')
  },
  {
    path: '/retrive',
    name: 'Retrive',
    component: () => import('../views/Retrive')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export const setRoutes = () => {
  // 查询用户获得授权的菜单
  let menus = JSON.parse(localStorage.getItem('user')).menuList
  if (menus) {
    // 设置路由
    let currentRouteNames = router.getRoutes().map(route => route.name)
    if (!currentRouteNames.includes('Manage')) {
      // 向主路由中注入子路由
      let manageRoute = { path: '/', redirect: '/login', name: 'Manage', component: Manage, children: [] }
      menus.forEach(menu => {
        if (menu.path) {
          const subMenu = { path: menu.path.replace("/", ""), name: menu.name, component: () => import('../views/' + menu.realName) }
          manageRoute.children.push(subMenu)
        } else if (menu.children) {
          menu.children.forEach(child => {
            const childMenu = { path: child.path.replace("/", ""), name: child.name, component: () => import('../views/' + child.realName) }
            manageRoute.children.push(childMenu)
          })
        }
      })
      manageRoute.children.push(
          { path: 'filedetails', name: '文件详情', component: () => import('../views/FileDetails') },
          { path: 'articledetail', name: '博文详情', component: () => import('../views/ArticleDetails') },
          { path: 'person', name: '个人信息', component: () => import('../views/Person') },
          { path: 'my-articles', name: '我的博文', component: () => import('../views/MyArticles') },
          { path: 'my-files', name: '我的文件', component: () => import('../views/MyFiles') }
      )
      router.addRoute(manageRoute)
    }
  }
}

// 在此刷新路由
setRoutes()

router.beforeEach((to, from, next) => {
  if (!to.matched.length) {
    const storageMenu = JSON.parse(localStorage.getItem('user')).menuList
    if (storageMenu) {
      next('/404')
    } else {
      next('/login')
    }
  }
  next()
})

export default router
